<?php
require_once ('simpletest/autorun.php');

require_once ('../savegoalfunction.php');
require_once ('../updategoalfunction.php');
require_once ('../deletegoalfunction.php');
require_once ('../validationfunctions.php');;

class TestDBFunctions extends UnitTestCase {
	private $dbconnection;

	function setUp() {
		$this -> dbconnection = mysql_connect("localhost", "web964", "pass17") or die("No connection to database.");
		mysql_select_db("usr_web964_1") or die("Database does not exist.");
	}

	function tearDown() {
		mysql_close($this -> dbconnection);
	}

	function testSaveAndDeleteGoal() {
		
		$this -> assertTrue(file_exists('../savegoalfunction.php'));

		# Test save function
		$_POST['subgoalname_0'] = "ABC-subgoal";
		$_POST['subgoalmeasurename_0'] = "seconds";
		$_POST['subgoalmeasuretype_0'] = "1";
		$_POST['subgoalstartvalue_0'] = "200";
		$_POST['subgoalcurrentvalue_0'] = "100";
		$_POST['subgoaltargetvalue_0'] = "20";
		$_POST['subgoalpriority_0'] = "nuni";
		$_POST['subgoaldeadline_0'] = "2012-12-30";
		$_POST['submit'] = "submit";
		$_POST['dummy1'] = "submit";
		$_POST['dummy2'] = "submit";
		$_POST['dummy3'] = "submit";
		$_POST['dummy4'] = "submit";
		$_POST['dummy5'] = "submit";
		$_POST['dummy6'] = "submit";
		$_POST['dummy7'] = "submit";
		$_POST['dummy8'] = "submit";
		$_POST['dummy9'] = "submit";

		saveGoalIntoDatabase("ABCDE", "Description", "0", "0", "100", "nuni", "%", "5", "2020-12-31", 2);

		$goalid = "";
	
		$result = mysql_query("SELECT * FROM goal WHERE name='ABCDE' AND fk_userid='2'");
		while ($row = mysql_fetch_object($result)) {
			$goalid = $row -> id;

			$this -> assertEqual($row -> name, "ABCDE");
			$this -> assertEqual($row -> description, "Description");
			$this -> assertEqual($row -> targetvalue, "100");
			$this -> assertEqual($row -> startvalue, "0");
			$this -> assertEqual($row -> value, "0");
			$this -> assertEqual($row -> measurename, "%");
			$this -> assertEqual($row -> priority, "nuni");
			$this -> assertEqual($row -> fk_userid, "2");
			$this -> assertEqual($row -> fk_measuretypeid, "5");
		}
		$subgoalid = "";
		
		
		$resultSubgoal = mysql_query("SELECT * FROM subgoal WHERE name='ABC-subgoal' AND fk_goalid='$goalid'");
		while ($row = mysql_fetch_object($resultSubgoal)) {
			$subgoalid = $row -> id;

			$this -> assertEqual($row -> name, "ABC-subgoal");
			$this -> assertEqual($row -> targetvalue, "20");
			$this -> assertEqual($row -> startvalue, "200");
			$this -> assertEqual($row -> value, "100");
			$this -> assertEqual($row -> measurename, "seconds");
			$this -> assertEqual($row -> priority, "unurgent / unimportant");
			$this -> assertEqual($row -> fk_goalid, $goalid);
			$this -> assertEqual($row -> fk_measuretypeid, "1");
		}

		$resultGoalDeadline = mysql_query("SELECT date, fk_goalid FROM goaldeadline WHERE fk_goalid='$goalid'");
		while ($row = mysql_fetch_object($resultGoalDeadline)) {
			$this -> assertEqual($row -> date, "2020-12-31");
			$this -> assertEqual($row -> fk_goalid, "$goalid");
		}

		$resultSubgoalDeadline = mysql_query("SELECT date, fk_subgoalid FROM subgoaldeadline WHERE fk_subgoalid='$subgoalid'");
		while ($row = mysql_fetch_object($resultSubgoalDeadline)) {
			$this -> assertEqual($row -> date, "2012-12-30");
			$this -> assertEqual($row -> fk_subgoalid, "$subgoalid");
		}

		# Test delete function

		deleteGoal($goalid);

		$resultDeleteGoal = mysql_query("SELECT id FROM goal WHERE id='$goalid'");
		$resultDeleteSubgoal = mysql_query("SELECT id FROM subgoal WHERE id='$subgoalid'");
		$resultDeleteGoalDeadline = mysql_query("SELECT id FROM goaldeadline WHERE fk_goalid='$goalid'");
		$resultDeleteSubgoalDeadline = mysql_query("SELECT id FROM subgoaldeadline WHERE fk_subgoalid='$subgoalid'");

		$this -> assertTrue(mysql_num_rows($resultDeleteGoal) == 0);
		$this -> assertTrue(mysql_num_rows($resultDeleteSubgoal) == 0);
		$this -> assertTrue(mysql_num_rows($resultDeleteGoalDeadline) == 0);
		$this -> assertTrue(mysql_num_rows($resultDeleteSubgoalDeadline) == 0);

	}

	function testUpdateGoal() {
		#Test update function
		$_SESSION['userid'] = "2";
		$_POST['subgoalname_0'] = "TestUpdateGoal_firstValue";
		$_POST['subgoalmeasurename_0'] = "seconds_firstValue";
		$_POST['subgoalmeasuretype_0'] = "1";
		$_POST['subgoalstartvalue_0'] = "200";
		$_POST['subgoalcurrentvalue_0'] = "100";
		$_POST['subgoaltargetvalue_0'] = "20";
		$_POST['subgoalpriority_0'] = "nuni";
		$_POST['subgoaldeadline_0'] = "2014-12-30";
		$_POST['submit'] = "submit";
		$_POST['dummy1'] = "submit";
		$_POST['dummy2'] = "submit";
		$_POST['dummy3'] = "submit";
		$_POST['dummy4'] = "submit";
		$_POST['dummy5'] = "submit";
		$_POST['dummy6'] = "submit";
		$_POST['dummy7'] = "submit";
		$_POST['dummy8'] = "submit";
		$_POST['dummy9'] = "submit";

		$idSavedGoalForUpdate = "";
		$idSavedSubgoalForUpdate = "";
		saveGoalIntoDatabase("Goal name_firstValue", "Description_firstValue", "0", "0", "100", "nuni", "%", "5", "2020-12-31", 2);

		$result = mysql_query("SELECT * FROM goal WHERE name='Goal name_firstValue' AND fk_userid='2'");
		while ($row = mysql_fetch_object($result)) {
			$idSavedGoalForUpdate = $row -> id;
		}
		$result = mysql_query("COMMIT");
		$result = mysql_query("SELECT * FROM subgoal WHERE name='TestUpdateGoal_firstValue' AND fk_goalid='$idSavedGoalForUpdate'");
		while ($row = mysql_fetch_object($result)) {
			$idSavedSubgoalForUpdate = $row -> id;
		}
		
		
		unset($_POST);
		$_POST['subgoalname_' . $idSavedSubgoalForUpdate] = "TestUpdateGoal_updated";
		$_POST['subgoalmeasurename_' . $idSavedSubgoalForUpdate] = "seconds_updated";
		$_POST['subgoalmeasuretype_' . $idSavedSubgoalForUpdate] = "1";
		$subgoalstart = "subgoalstart_" . "$idSavedSubgoalForUpdate";
		$_POST["subgoalstart_" . "$idSavedSubgoalForUpdate"] = "200";
		$_POST['subgoalcurrent_' . $idSavedSubgoalForUpdate] = "100";
		$_POST['subgoaltarget_' . $idSavedSubgoalForUpdate] = "20";
		$_POST['subgoalpriority_' . $idSavedSubgoalForUpdate] = "ui";
		$_POST['subgoaldeadline_' . $idSavedSubgoalForUpdate] = "2015-01-01";
		$_POST['submit'] = "submit";
		$_POST['dummy1'] = "submit";
		$_POST['dummy2'] = "submit";
		$_POST['dummy3'] = "submit";
		$_POST['dummy4'] = "submit";
		$_POST['dummy5'] = "submit";
		$_POST['dummy6'] = "submit";
		$_POST['dummy7'] = "submit";
		$_POST['dummy8'] = "submit";
		$_POST['dummy9'] = "submit";
		
		updateGoal($idSavedGoalForUpdate, "Updated goal name 78910", "Updated description", 0, 0, 100, "ui", "Updated measure name", "5", "2021-12-31");

		$idUpdatedGoal = "";
		$result = mysql_query("SELECT * FROM goal WHERE name='Updated goal name 78910' AND fk_userid='2'");
		while ($row = mysql_fetch_object($result)) {
			$idUpdatedGoal = $row -> id;

			$this -> assertEqual($row -> name, "Updated goal name 78910");
			$this -> assertEqual($row -> description, "Updated description");
			$this -> assertEqual($row -> targetvalue, "100");
			$this -> assertEqual($row -> startvalue, "0");
			$this -> assertEqual($row -> value, "0");
			$this -> assertEqual($row -> measurename, "Updated measure name");
			$this -> assertEqual($row -> priority, "ui");
			$this -> assertEqual($row -> fk_userid, "2");
			$this -> assertEqual($row -> fk_measuretypeid, "5");
		}
		
		
	
		$result = mysql_query("SELECT * FROM goal WHERE id='$idSavedGoalForUpdate'");
		$this -> assertTrue(mysql_num_rows($result) == 0);
		$result = mysql_query("SELECT * FROM subgoal WHERE fk_goalid='$idSavedGoalForUpdate'");
		$this -> assertTrue(mysql_num_rows($result) == 0);
		$result = mysql_query("SELECT * FROM goaldeadline WHERE fk_goalid='$idSavedGoalForUpdate'");
		$this -> assertTrue(mysql_num_rows($result) == 0);
		$result = mysql_query("SELECT * FROM subgoaldeadline WHERE fk_subgoalid='$idSavedSubgoalForUpdate'");
		$this -> assertTrue(mysql_num_rows($result) == 0);
		
		# clear
		deleteGoal($idUpdatedGoal);

	}

}
?>